package movies
{
	import mymyoux.io.LoadAdvTextFile;
	import mymyoux.utils.Hash;
	
	import org.osflash.signals.Signal;
	
	/**
	 * Allocine Parser
	 */
	public class Allocine
	{
		protected var searches:Hash;
		public function Allocine()
		{
			searches = new Hash();
		}
		public function searchMovie(movie:String):Signal
		{
			var s:Signal = new Signal(String,String);
			searches.put(movie,s);
			var loader:LoadAdvTextFile = new LoadAdvTextFile("http://www.allocine.fr/recherche/?q="+movie);
			loader.onCompleted.addOnce(onSearch);
			return s;
		}
		protected function onSearch(status:String,url:String,value:String):void
		{
			if(status==LoadAdvTextFile.COMPLETED)
			{
				var search:AllocineSearch = new AllocineSearch();
				value = value.replace(new RegExp("\r","g"),"");
				value = value.replace(new RegExp("\n","g"),"");
				value = value.replace(new RegExp("\r\n","g"),"");
				
				//catégories
				var regexp:RegExp = new RegExp("<div class=\"clmmastertopic\">[^<]*<a href='/recherche/([0-9]+/)?\\?q=[^']+'>[^<]*<img id=\"EmptyImage1\" alt=\" \" width=\"0\" height=\"0\" src=\"http://images.allocine.fr/commons/empty.gif\"/>([^<]+)<span class=\"extra\">\\(([0-9]+)\\)",'ig');
         		var result:Object = regexp.exec(value);
                while (result != null) {
			     	search.addCategorie(result[2],result[3]);
			     	result = regexp.exec(value);
			    }       
			    //résultats page 1
			    regexp = new RegExp("<tr>[^<]*<td style=\" vertical-align:middle;\">[^<]*<a href='[^']+'>[^<]*<img[^s]+src='[^']+'[^a]+alt='[^']+'[^/]+/>[^<]*</a>[^<]*</td>[^<]*<td style=\" vertical-align:top;\" class=\"totalwidth\">[^<]*<div>[^<]*<div style=\"margin-top:-5px;\">[^<]*<a href='/([^/]+)/([^']+)'> *([^<]*)<b>([^<]+)</b>([^<]*)</a>","gi"); 
                                                                                
			    result = regexp.exec(value);
			    while (result != null) {
			     	search.addIntoCategorie(result[1],result[2],result[3]+""+result[4]+""+result[5]);
			     	result = regexp.exec(value);
			    }
			   trace(search);
			}else
			{
				trace("hum");
			}
		//	trace(status+":"+url+"\n"+value);
		}
	}
}